const API_BASE = process.env.NEXT_PUBLIC_API_BASE ?? "http://localhost:8080";

async function post(path: string, body: unknown) {
  const res = await fetch(`${API_BASE}${path}`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(body)
  }).catch(() => null);
  if (!res || !res.ok) {
    throw new Error(`Action failed (${path})`);
  }
  return res.json().catch(() => ({}));
}

export async function issueCredits(account: string, amount: number) {
  return post(`/accounts/${account}/issue`, { amount, reason: "console_action" });
}

export async function chargeCredits(account: string, amount: number) {
  return post(`/accounts/${account}/charge`, { amount, reason: "console_action" });
}

export async function createGovernanceTask(owner: string, actor: string, title: string) {
  return post(`/governance/tasks`, { owner, actor, title });
}
